package kingwing.dc.core.sevice.quartz.job;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import kingwing.dc.core.model.Infos;
import kingwing.dc.core.model.JobTimeRule;
import kingwing.dc.core.model.Rule;
import kingwing.dc.core.model.RuleDetail;
import kingwing.dc.core.orm.hibernate.CommonHibernateDao;
import kingwing.dc.core.sevice.HTMLProcessor;
import kingwing.dc.core.sevice.JSProcessor;
import kingwing.dc.core.sevice.quartz.dao.JObHibernateDao;
import kingwing.dc.core.sevice.quartz.sevice.QuartzManager;
import kingwing.dc.core.view.rule.RulePR;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.transaction.annotation.Transactional;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ResultItemsCollectorPipeline;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.Expose;

/**
 * 如果要保证添加的站点可用，
 * 那么这个定时任务就必须存在！
 * 因为它的用处是对任务表进行扫描的。
 * 当发现任务表里存在要执行的定时任务时，
 * 它就会把任务表的任务启动起来！
 * @author Kevin
 * 2017年10月25日
 */
public class job implements Job {
	
	@Resource(name=CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao commonHibernateDao;

	@Resource(name="quartzManager")
	private QuartzManager quartzManager;
	
	@SuppressWarnings("unchecked")
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		
		DetachedCriteria detachedCriteriaJobTimeRule = DetachedCriteria.forClass(JobTimeRule.class);
		List<JobTimeRule> jobTimeRules = (List<JobTimeRule>) this.commonHibernateDao.query(detachedCriteriaJobTimeRule);
		List<Map<String, Object>> jobs = quartzManager.queryAllJob(); //获取正在运行的任务
		boolean flag =false; //用来判断扫描得到的任务表中，是否已有存在运行的任务的标识符
		
		if (jobTimeRules!=null && jobTimeRules.size()!=0) {
			for (JobTimeRule jobTimeRule : jobTimeRules) {
				if (jobs==null || jobs.isEmpty()) {
					quartzManager.addJob(JobSite.class, jobTimeRule.getJobName(), jobTimeRule.getCreateUserId(), jobTimeRule.getJobTime(), -1,jobTimeRule,jobTimeRule.getKeyWord());
				}else {
					for (Map<String, Object> map : jobs) {
						if (map.get("jobGroupName").equals(jobTimeRule.getCreateUserId()) &&
								map.get("jobName").equals(jobTimeRule.getJobName())){
							flag = true;
						}
					}
					
					if (!flag) {
						quartzManager.addJob(JobSite.class, jobTimeRule.getJobName(), jobTimeRule.getCreateUserId(), jobTimeRule.getJobTime(), -1,jobTimeRule,jobTimeRule.getKeyWord());
					}
				}
				flag = false; 
			}
		}
		
		Date date=new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sf.format(date)+":扫描任务表");

	}
	
}
